// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    searchBarStatus:true,
    userLocation:false,
    isOpeningset:false,

    width:0,
    rpx:0,
    height:0,
    tabbarHeight:0,

    startAddr:'',
    desAddr:'',
    polyline:[],
    markers:[],
    startLat:'',
    startLng:'',
    desLat:'',
    desLng:'',
    cur_lng:'',
    cur_lat:'',

    scale:16,

    daysSelected:0,
    showPoints:[],
    riskPoints7:[],
    riskPoints14:[],
  },

  onLoad() {
    let that = this
    this.getRiskPoints(7)
    this.getRiskPoints(14)
    wx.showLoading({
      title: '加载风险点信息中',
    })

    setTimeout(function (){
      wx.hideLoading({
        success: (res) => {that.showPoints(14),that.getLocation()},
      })
    },500)
    
  },

  onReady(){
    let that = this
    let {width,height,rpx} = that
    width = wx.getSystemInfoSync().windowWidth
    rpx = this.data.width/750
    height = wx.getSystemInfoSync().windowHeight
    this.setData({
      width:width,
      height:height,
      rpx:rpx,
      tabbarHeight : 120*rpx
    })
  },

  onShow(){
  },

  onHide(){
    this.data.cen_lat=''
    this.data.cen_lng=''
  },

  getInput(e){
    
    let id = e.currentTarget.id
    if(id == "input1"){
      this.data.startAddr = e.detail.value
    }
    else{
      this.data.desAddr = e.detail.value
    }
  },

  getLocation(){
    let that = this
    let map = wx.createMapContext('map')

    if(that.data.userLocation == false || that.data.cen_lng == ''){
      wx.getLocation({
        type: 'gcj02',
        success: (result) => {
          that.data.userLocation = true
          that.data.cen_lng = result.longitude
          that.data.cen_lat = result.latitude
          map.moveToLocation({
            longitude:result.longitude,
            latitude:result.latitude,
          })
        },
        fail: (res) => {
          wx.getSetting({
            success(res){
              if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']){
                wx.showModal({
                  title:"提示",
                  content:"您拒绝了定位权限，将无法使用定位功能。是否重新授权",
                  success(res){ 
                    if(res.confirm){
                      wx.openSetting({
                        success(res){
                          if(res.authSetting["scope.userLocation"]){
                            that.data.userLocation=true,
                            wx.getLocation({
                              type: 'gcj02',
                              success: (result) => {
                                that.data.userLocation = true
                                that.data.cen_lng = result.longitude
                                that.data.cen_lat = result.latitude
                                map.moveToLocation({
                                  longitude:result.longitude,
                                  latitude:result.latitude,
                                })
                              }
                            })
                          }
                          else{
                            wx.showToast({
                              title: '没有允许定位权限',
                              icon:"error"
                            })
                            map.moveToLocation({
                              longitude:116.39713168144226,
                              latitude:39.917298837410975
                            })
                          }
                        },
                      })
                    }
                    else{
                      map.moveToLocation({
                        longitude:116.39713168144226,
                        latitude:39.917298837410975
                      })
                    }
                  }
                })
              }
            },
          })
        }  
      })
    }
    else{
      map.moveToLocation({
        longitude:that.data.longitude,
        latitude:that.data.latitude,
      })
    }
  },
  
  chooseLocation(e){
    let map = wx.createMapContext('map')
    let id = e.currentTarget.id
    let that = this
    map.getCenterLocation({
      success(res){
        wx.chooseLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          success(res){
            if(id == "input1"){
              that.data.startLat = res.latitude
              that.data.startLng = res.longitude
              that.setData({
                startAddr : res.name + res.address
              })
            }
            else{
              that.data.desLat = res.latitude
              that.data.desLng = res.longitude
              that.setData({
                desAddr : res.name + res.address
              })
            }
          }
        })
      }
    })
  },

  getPath(){
    let that = this
    let {startAddr,desAddr,startLat,startLng,desLat,desLng} = this.data
    let map = wx.createMapContext('map')
    
    if(startAddr == '' || desAddr == ''){
      wx.showModal({
        title:"输入有误",
        content:"起始地址或者目的地址不能为空",
        showCancel:false,
        confirmText:"我已了解",
      })
      return
    }

    startAddr = "坐标"
    desAddr = "坐标"
    
    wx.request({
      url: 'https://epidemic-notification.alisssqaq.cn/pathplan',
      method:'get',
      header:{
        'content-type':'x-www-form-urlencoded'
      },
      data:{
        origin:startAddr,
        destination:desAddr,
        orilng:startLng,
        orilat:startLat,
        deslng:desLng,
        deslat:desLat
      },
      success(res){
        console.log(res)
        let data = JSON.parse(res.data)
        let polyline = []
        let markers = []
        for(let i=0;i<data.polyline.length;i++){
          let points = data.polyline[i].split(',')
          polyline.push({
              latitude:points[1],
              longitude:points[0]
          })
          if(i == 0){
            markers.push({
            id:i,
            // title:"起点",
            latitude:points[1],
            longitude:points[0],
            iconPath:"/image/icon-startPoint.png",
            width:50,
            height:50
          })
          }
          if(i == data.polyline.length-1){
            markers.push({
            id:i,
            // title:"终点",
            latitude:points[1],
            longitude:points[0],
            iconPath:"/image/icon-destinationPoint.png",
            width:50,
            height:50
          })
          }
        }
        that.setData({
          polyline:[{
            points:polyline,
            color:"#3282f6",
            width:3,
            arrowline:true
          }],
          markers:markers,
          searchBarStatus : ! that.data.searchBarStatus
        })
        console.log(polyline[0].longitude + "  " + polyline[0].latitude)
        map.moveToLocation({
          longitude:Number(polyline[0].longitude),
          latitude:Number(polyline[0].latitude)
        })
        // console.log(that.data.polyline)
      }
    })

  },

  switchSearchBarStatus(e){
    let {searchBarStatus} = this.data
    this.setData({
      searchBarStatus:!searchBarStatus 
    })
  },

  getRiskPoints(days){
    let that = this
    let tem_days=(days==7)?14:21
    wx.request({
      url: 'https://epidemic-notification.alisssqaq.cn/riskpoints',
      method: 'get',
      header:{
        'content-type':'x-www-form-urlencoded'
      },
      data:{
        "days":tem_days
      },
      success(res){
        let data = JSON.parse(res.data) 
        if(days == 7){
          for(let points of data){
            that.data.riskPoints7.push({
              "latitude":points.lat,
              "longitude":points.lng,
              "radius":150,
              "fillColor":"#ff575790",
              "color":"#ff0000"
            })
          }
        }
        else{
          for(let points of data){
            let color =  "#ffc25790"
            for(let points7 of that.data.riskPoints7){
              if(points7.latitude == points.lat && points7.longitude == points.lng){
                color = "#ff575790";
                break;
              }
            }
            that.data.riskPoints14.push({
              "latitude":points.lat,
              "longitude":points.lng,
              "radius":150,
              "fillColor":color,
              "color":"#ff0000"
            })
          }
        }
      },
      fail(res){
        console.log(res)
      }
    })
  },

  showPoints(e){
    let {daysSelected} = this.data

    let days = null
    if(typeof(e) == "object")
      days = e.currentTarget.dataset.days
    else
      days = e
    if(days == 7){
     if(daysSelected != 7){
      this.setData({
        daysSelected:7,
        showPoints:this.data.riskPoints7
      })
     }
   }
   else{
    if(daysSelected != 14){
      this.setData({
        daysSelected:14,
        showPoints:this.data.riskPoints14
      })
     }
   }
  },

  changeScale(e){
    let map = wx.createMapContext('map')
    let id = e.currentTarget.id
    let that = this
    map.getScale({
      success(res){
        if(id == "scale-add"){
          that.setData({
            scale : Math.min(20, res.scale+1)
          })
        }
        else{
          that.setData({
            scale : Math.max(3, res.scale-1)
          })
        }
      }
    })
  }
})
